package apidoc

import (
	"autoTestNew/pkg/config"
	autotest "autoTestNew/pkg/dashboard/autoTest"
	apidoc "autoTestNew/pkg/mongo/autoTestNew/apiDoc"
	"autoTestNew/pkg/mongo/autoTestNew/record"
	"autoTestNew/pkg/mongo/autoTestNew/task"
	"autoTestNew/pkg/mongo/autoTestNew/testCase"
	"autoTestNew/pkg/mongo/autoTestNew/testMicroservice"
	"github.com/gin-gonic/gin"
	"github.com/go-logr/logr"
)

// Service defines a handler service for experiments.
type Service struct {
	autoTest              *autotest.AutoTest
	apiDocStore           *apidoc.Store
	recordStore           *record.Store
	taskStore             *task.Store
	testCaseStore         *testCase.Store
	testMicroserviceStore *testMicroservice.Store
	config                *config.ChaosDashboardConfig
	log                   logr.Logger
}

func NewService(
	autoTest *autotest.AutoTest,
	apiDocStore *apidoc.Store,
	recordStore *record.Store,
	taskStore *task.Store,
	testCaseStore *testCase.Store,
	testMicroserviceStore *testMicroservice.Store,
	config *config.ChaosDashboardConfig,
	log logr.Logger,
) *Service {
	return &Service{
		autoTest:              autoTest,
		apiDocStore:           apiDocStore,
		testCaseStore:         testCaseStore,
		recordStore:           recordStore,
		testMicroserviceStore: testMicroserviceStore,
		taskStore:             taskStore,
		config:                config,
		log:                   log,
	}
}

// Register autotest RouterGroup.
func Register(r *gin.RouterGroup, s *Service) {
	RegisterFrontEndAPI(r, s)
	RegisterJavaEndAPI(r, s)
}

func RegisterJavaEndAPI(r *gin.RouterGroup, s *Service) {
	endpoint := r.Group("/auto_test")

	// 创建和执行测试任务
	endpoint.POST("/startTest", s.startTestTaskFromMongoDB)

	//// 获取各种类型的默认 Chaos Mesh 对象模板
	//endpoint.GET("/chaos/:chaos_type", s.getChaos)

	// 测试任务相关
	{
		// 创建测试任务
		endpoint.POST("/addTask", s.createTestTask)
		// 执行测试任务
		endpoint.POST("/executeTask", s.executeTestTask)
		// 测试请求
		endpoint.GET("/testRequest", s.testRequest)
	}
}

func RegisterFrontEndAPI(r *gin.RouterGroup, s *Service) {
	endpoint := r.Group("/autotest")

	// 由选择的服务创建测试任务
	endpoint.POST("/createTestTask", s.createTestTask)
}
